package cn.exrick.xboot.base.serviceimpl

import cn.exrick.xboot.base.dto.EnterpriseTrainingNeedDto
import cn.exrick.xboot.base.entity.EnterpriseTrainingNeeds
import cn.exrick.xboot.base.mapper.EnterpriseTrainingNeedsMapper
import cn.exrick.xboot.base.service.IBusinessService
import cn.exrick.xboot.base.service.IEnterpriseTrainingNeedsService
import cn.exrick.xboot.base.service.IEnterpriseUserService
import cn.exrick.xboot.base.vo.EnterpriseTrainingNeedsListVo
import cn.exrick.xboot.core.common.utils.PageUtil
import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.utils.SecurityUtil
import cn.exrick.xboot.core.common.utils.ToolUtil
import cn.exrick.xboot.core.common.vo.PageVo
import cn.exrick.xboot.core.common.vo.Result
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.google.common.collect.Maps
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * 企业培训需求接口实现
 * @author Fei
 */
@Slf4j
@Service
@Transactional
open class IEnterpriseTrainingNeedsServiceImpl : ServiceImpl<EnterpriseTrainingNeedsMapper?, EnterpriseTrainingNeeds?>(), IEnterpriseTrainingNeedsService {


    @Autowired
    private val securityUtil: SecurityUtil? = null

    @Autowired
    private lateinit var iBusinessService: IBusinessService

    @Autowired
    private lateinit var iEnterpriseUserService: IEnterpriseUserService


    override fun publishOrUpdateEnterpriseTrainingNeed(enterpriseTrainingNeedDto: EnterpriseTrainingNeedDto?): Result<Any?>? {

       return enterpriseTrainingNeedDto?.let {

            EnterpriseTrainingNeeds().apply {

                ToolUtil.copyProperties(it, this)
                enterpriseId = iEnterpriseUserService.getEnterpriseIdByEmployeeUserId(securityUtil!!.currUser.id)
                saveOrUpdate(this)
            }

           return ResultUtil.success("发布成功！")
        } ?: ResultUtil.error("发布失败,缺失参数！")
    }

    override fun getMyPublishEnterpriseTrainingNeedList(pageVo: PageVo): Result<MutableMap<String, Any?>?> {

       return ToolUtil.setListToNul(list(KtQueryWrapper(EnterpriseTrainingNeeds::class.java).eq(EnterpriseTrainingNeeds::enterpriseId, iEnterpriseUserService.getEnterpriseIdByEmployeeUserId(securityUtil!!.currUser.id) )
                .orderByDesc(EnterpriseTrainingNeeds::createTime)))?.let {

            val enterpriseTrainingNeedsListVo = it.map { enterpriseTrainingNeeds ->

                getEnterpriseTrainingNeedsListVo(enterpriseTrainingNeeds!!)
            }.toMutableList()

            val result: MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = enterpriseTrainingNeedsListVo.size
            result["content"] = PageUtil.listToPage(pageVo, enterpriseTrainingNeedsListVo)

            return ResultUtil.data(result, "获取数据成功")
        } ?: ResultUtil.error(201,"暂无数据")
    }

    override fun getEnterpriseTrainingNeedListByAreaId(pageVo: PageVo): Result<MutableMap<String, Any?>>? {

        if (ToolUtil.isEmpty(iEnterpriseUserService.getEnterpriseIdByAreaId(securityUtil!!.currUser.areaId))) {

            return ResultUtil.error(201, "暂无数据！")
        }

        return ToolUtil.setListToNul(list(KtQueryWrapper(EnterpriseTrainingNeeds::class.java).`in`(EnterpriseTrainingNeeds::enterpriseId,
                iEnterpriseUserService.getEnterpriseIdByAreaId(securityUtil.currUser.areaId) )
                .orderByDesc(EnterpriseTrainingNeeds::createTime)))?.run {

            val result: MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = size
            result["content"] = PageUtil.listToPage(pageVo, mapNotNull { enterpriseTrainingNeeds ->
                getEnterpriseTrainingNeedsListVo(enterpriseTrainingNeeds!!)
            }.toMutableList())

            return ResultUtil.data(result, "获取数据成功")
        } ?: ResultUtil.error(201,"暂无数据")
    }

    private fun getEnterpriseTrainingNeedsListVo(enterpriseTrainingNeeds: EnterpriseTrainingNeeds): EnterpriseTrainingNeedsListVo {

        return EnterpriseTrainingNeedsListVo().apply {
            ToolUtil.copyProperties(enterpriseTrainingNeeds, this)
            enterpriseUserListVo = iEnterpriseUserService.getEnterpriseUserVo(enterpriseTrainingNeeds.enterpriseId!!)
            professionalTypes = enterpriseTrainingNeeds.professionalTypes?.split(",")?.mapNotNull { iBusinessService.getBusinessNameById(it) }?.toMutableList()
            professionalTypesStr = enterpriseTrainingNeeds.professionalTypes?.let {
                iBusinessService.listByIds(it.split(",").toList()).mapNotNull { business -> business!!.businessName }.joinToString(",")
            }
        }
    }

    override fun removePublishEnterpriseTrainingNeed(ids: Array<out String>): Result<Any?>? {

        removeByIds(ids.toList())

        return ResultUtil.success("移除成功！")
    }


}